package cn.itcast.jx.action.cargo;

import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.common.SysConstant;
import cn.itcast.jx.domain.Contract;
import cn.itcast.jx.domain.Export;
import cn.itcast.jx.domain.ExportProduct;
import cn.itcast.jx.domain.ExtEproduct;
import cn.itcast.jx.service.ContractService;
import cn.itcast.jx.service.ExportProductService;
import cn.itcast.jx.service.ExportService;
import cn.itcast.jx.service.ExtEproductService;
import cn.itcast.jx.util.Page;
import cn.itcast.jx.util.UtilFuns;
import cn.itcast.jx.vo.ExportProductResult;
import cn.itcast.jx.vo.ExportProductVo;
import cn.itcast.jx.vo.ExportResult;
import cn.itcast.jx.vo.ExportVo;

public class ExportAction extends BaseAction  implements ModelDriven<Export>{

	private Export model = new Export();
	private ExportService exportService;
	
	
	private ContractService contractService;
	private String menuType = "export";
	private Page page =new Page();
	
	private String[] mr_id;
	private Integer[] mr_changed;
	private Integer[] mr_cnumber;
	private Double[] mr_grossWeight;
	private Double[] mr_netWeight;
	private Double[] mr_sizeLength;
	private Double[] mr_sizeWidth;
	private Double[] mr_sizeHeight;
	private Double[] mr_exPrice;
	private Double[] mr_tax;
	
	private ExportProductService exportProductService;
	
	public String contractList() throws Exception {
		String hql = "from Contract where state = 2";
		
		contractService.findPage(hql, page, Contract.class, null);
		page.setUrl("exportAction_contractList");
		push(page);
		return "contractList";
	}
	
	public String tocreate() {
		String contractIds = model.getId();
		push(contractIds);
		return "tocreate";
	}
	
	public String insert() {
		
		exportService.saveOrUpdate(model);
		
		return "insert";
	}
	
	public String list() {
		
		exportService.findPage("from Export", page, Export.class, null);
		page.setUrl("exportAction_list");
		push(page);
		
		return "list";
	}
	
	public String toupdate() {
		Export export = exportService.get(Export.class, model.getId());
		//addTRRecord("mRecordTable", id, productNo, cnumber, grossWeight, 
		//netWeight, sizeLength, sizeWidth, sizeHeight, exPrice, tax)
		Set<ExportProduct> expSet = export.getExportProducts();
		
		StringBuffer sb = new StringBuffer();
		
		for (ExportProduct exp : expSet) {
			sb.append("addTRRecord('mRecordTable',");
			sb.append("'"+exp.getId()+"',");
			sb.append("'" + UtilFuns.convertNull(exp.getProductNo()) + "',");
			sb.append("'" + UtilFuns.convertNull(exp.getCnumber()) + "',");
			sb.append("'" + UtilFuns.convertNull(exp.getGrossWeight()) + "',");
			sb.append("'" + UtilFuns.convertNull(exp.getNetWeight()) + "',");
			sb.append("'" + UtilFuns.convertNull(exp.getSizeLength()) + "',");
			sb.append("'" + UtilFuns.convertNull(exp.getSizeWidth() )+ "',");
			sb.append("'" + UtilFuns.convertNull(exp.getSizeHeight()) + "',");
			sb.append("'" + UtilFuns.convertNull(exp.getExPrice()) + "',");
			sb.append("'" + UtilFuns.convertNull(exp.getTax()) + "');");
		}
		put("mRecordData", sb.toString());
		
		push(export);
		return "toupdate";
	}
	
	
	public String toview() {
		Export export = exportService.get(Export.class, model.getId());
		push(export);
		
		return "toview";
	}
	
	public String delete() {
		String[] ids = model.getId().split(", ");
		
		exportService.delete(Export.class, ids);
		
		return SUCCESS;
	}
	public String update() {
		Export export = exportService.get(Export.class, model.getId());
		export.setInputDate(model.getInputDate());
		export.setLcno(model.getLcno());
		export.setConsignee(model.getConsignee());
		export.setShipmentPort(model.getShipmentPort());
		export.setDestinationPort(model.getDestinationPort());
		export.setTransportMode(model.getTransportMode());
		export.setPriceCondition(model.getPriceCondition());
		export.setMarks(model.getMarks());
		export.setRemark(model.getRemark());
		
		HashSet<ExportProduct> expSet = new HashSet<ExportProduct>();
		
		for(int i = 0;i<mr_id.length;i++) {
			ExportProduct exp = exportProductService.get(ExportProduct.class, mr_id[i]);
			
			if(mr_changed[i] != null && mr_changed[i] == 1) {
				exp.setCnumber(mr_cnumber[i]);
				exp.setGrossWeight(mr_grossWeight[i]);
				exp.setNetWeight(mr_netWeight[i]);
				exp.setSizeLength(mr_sizeLength[i]);
				exp.setSizeWidth(mr_sizeWidth[i]);
				exp.setSizeHeight(mr_sizeHeight[i]);
				exp.setExPrice(mr_exPrice[i]);
				exp.setTax(mr_tax[i]);
				
			}
			expSet.add(exp);
		}
		export.setExportProducts(expSet);
		
		exportService.saveOrUpdate(export);
		
		return SUCCESS;
	}
	
	public String export() throws Exception {
		Export export = exportService.get(Export.class, model.getId());
		ExportVo expVo = new ExportVo();
		BeanUtils.copyProperties(export, expVo);
		expVo.setExportId(export.getId());
		Set<ExportProduct> exportProducts = export.getExportProducts();
		
		Set<ExportProductVo> expVos = new HashSet<>();
		
		for (ExportProduct exp : exportProducts) {
			ExportProductVo expvo = new ExportProductVo();
			BeanUtils.copyProperties(exp, expvo);
			expvo.setExportProductId(exp.getId());
			expVos.add(expvo);
		}
		expVo.setProducts(expVos);
		
		WebClient.create("http://localhost:8881/CustomsReportSystem/ws/teleExport/export")
				.type(MediaType.APPLICATION_JSON)
				.post(expVo);
		
		ExportResult exportResult = WebClient.create("http://localhost:8881/CustomsReportSystem/ws/teleExport/export/"+model.getId())
				.type(MediaType.APPLICATION_JSON)
				.get(ExportResult.class);
		
		export.setState(exportResult.getState());
		export.setRemark(exportResult.getRemark());
		
		List<ExportProductResult> list = exportResult.getList();
		Set<ExportProduct> eps = new HashSet<>();
		for (ExportProductResult epRs : list) {
			ExportProduct ep = exportProductService.get(ExportProduct.class, epRs.getExportProductId());
			ep.setTax(epRs.getTax());
			eps.add(ep);
		}
		export.setExportProducts(eps);
		exportService.saveOrUpdate(export);
		
		
		return SUCCESS;
	}
	
	
	
	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	public void setExportService(ExportService exportService) {
		this.exportService = exportService;
	}

	@Override
	public Export getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	//分页组件

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	public String getMenuType() {
		return menuType;
	}
	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}

	public String[] getMr_id() {
		return mr_id;
	}

	public void setMr_id(String[] mr_id) {
		this.mr_id = mr_id;
	}

	public Integer[] getMr_changed() {
		return mr_changed;
	}

	public void setMr_changed(Integer[] mr_changed) {
		this.mr_changed = mr_changed;
	}

	public Integer[] getMr_cnumber() {
		return mr_cnumber;
	}

	public void setMr_cnumber(Integer[] mr_cnumber) {
		this.mr_cnumber = mr_cnumber;
	}

	public Double[] getMr_grossWeight() {
		return mr_grossWeight;
	}

	public void setMr_grossWeight(Double[] mr_grossWeight) {
		this.mr_grossWeight = mr_grossWeight;
	}

	public Double[] getMr_netWeight() {
		return mr_netWeight;
	}

	public void setMr_netWeight(Double[] mr_netWeight) {
		this.mr_netWeight = mr_netWeight;
	}

	public Double[] getMr_sizeLength() {
		return mr_sizeLength;
	}

	public void setMr_sizeLength(Double[] mr_sizeLength) {
		this.mr_sizeLength = mr_sizeLength;
	}

	public Double[] getMr_sizeWidth() {
		return mr_sizeWidth;
	}

	public void setMr_sizeWidth(Double[] mr_sizeWidth) {
		this.mr_sizeWidth = mr_sizeWidth;
	}

	public Double[] getMr_sizeHeight() {
		return mr_sizeHeight;
	}

	public void setMr_sizeHeight(Double[] mr_sizeHeight) {
		this.mr_sizeHeight = mr_sizeHeight;
	}

	public Double[] getMr_exPrice() {
		return mr_exPrice;
	}

	public void setMr_exPrice(Double[] mr_exPrice) {
		this.mr_exPrice = mr_exPrice;
	}

	public Double[] getMr_tax() {
		return mr_tax;
	}

	public void setMr_tax(Double[] mr_tax) {
		this.mr_tax = mr_tax;
	}

	public void setExportProductService(ExportProductService exportProductService) {
		this.exportProductService = exportProductService;
	}
	private ExtEproductService extEproductService;

	public void setExtEproductService(ExtEproductService extEproductService) {
		this.extEproductService = extEproductService;
	}
	
}
